var loginid,loginuser,loginname,loginkey;

var isjump = 0;//控制翻页
var layer,$,laydate,upload,element,form,loadmsg,edite_productid,product_image;
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'layer', 'element','upload'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    //laydate = layui.laydate;//日期
    upload = layui.upload;//上传
    element = layui.element; //元素操作
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	select_share_info();
});
/*查询分享信息*/
function select_share_info(){
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	var infos="";
	console.log(loginname + "','" + loginkey);
	$.ajax({
		url: "../../../../service",
		cache: false,
		async:false,
		data: {
			proc: "xmy_zyr.select_share_info",
			para: "['" + loginname + "','" + loginkey + "']"
		},
		dataType: "json",
		success: function(data) {
			console.log(JSON.stringify(data));
			$("#title_share").val(data[0].SHARE_TITLE);//请输入分享标题
			$("#share_url").val(data[0].SHARE_IMG);//请输入活动详情页背景图
			$("#share_concent").val(data[0].SHARE_CON);//请输入分享内容
			$("#card_end_time").val(data[0].END_TIME);
			$("#img_info").attr("src","/img_active/"+data[0].SHARE_IMG);
			var new_strs=data[0].NEW_STR;
			var old_strs=data[0].OLD_STR;
			var rule="";
			if(new_strs!=null){
				var new_str=new_strs.split(",");
				for(var i=0;i<new_str.length;i++){
					rule='<div class="rules margin_buttom10 display_table">'+
							'<div class="float_left">'+
							'<span class="margin_right10">优惠券类型</span>'+
								'<select class="width30 card_info">'+select_all_card(new_str[i].split(":")[0])+'</select>'+
							'<span class="margin_right10"> ,可以获得金额</span><input type="text" value="'+new_str[i].split(":")[1]+'" class="form-control form-boxed width20 money_set" /><span></span>'+
							'</div>'+
							'<div class="float_left padding6 bg_red" onclick="icon_remove(this)"><i class="icon-remove"></i></div>'+
						  '</div>';
					$("#rules_new").after(rule);
				}
			}
			if(old_strs!=null){
				var old_str=old_strs.split(",");
				for(var i=0;i<old_str.length;i++){
					rule='<div class="rules margin_buttom10 display_table">'+
							'<div class="float_left">'+
							'<span class="margin_right10">优惠券类型</span>'+
								'<select class="width30 card_info">'+select_all_card(new_str[i].split(":")[0])+'</select>'+
							'<span class="margin_right10"> ,可以获得金额</span><input type="text" value="'+old_str[i].split(":")[1]+'" class="form-control form-boxed width20 money_set" /><span></span>'+
							'</div>'+
							'<div class="float_left padding6 bg_red" onclick="icon_remove(this)"><i class="icon-remove"></i></div>'+
						  '</div>';
					$("#rules_old").after(rule);

				}
			}
		}
	});
}

//查询所有优惠卷类型
function select_all_card(val_id){
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	var infos="";
	$.ajax({
		url: "../../../../service",
		cache: false,
		async:false,
		data: {
			proc: "xmy_zyr.select_all_card",
			para: "['" + loginname + "','" + loginkey + "']"
		},
		dataType: "json",
		success: function(data) {
			//console.log(JSON.stringify(data));
			infos='<option value="0">请选择优惠券</option>';
			for(var i=0;i<data.length;i++){
				if(val_id==data[i].ID){
					infos+='<option selected="selected" value="'+data[i].ID+'">'+data[i].TYPENAME+'优惠券</option>';
				}else{
					infos+='<option value="'+data[i].ID+'">'+data[i].TYPENAME+'优惠券</option>';
				}
			}
		}
	});
	return infos;
}
function submit_share_config(){
	var title_share=$("#title_share").val();//请输入分享标题
	var share_url=$("#share_url").val();//请输入活动详情页背景图
	var share_concent=$("#share_concent").val();//请输入分享内容
	var card_end_time=$("#card_end_time").val();//请输入分享内容
	var new_oldstr="";
	var re_count=0;
	$("#rules_new").nextAll().each(function(){
		var select_in=$(this).find(".card_info option:selected").val();
		if(select_in==0||select_in=='0'){
			alert("请选择新用户得到优惠券类型!");
			//layer.closeAll();
			re_count =1;
			return ;
		}
		var money_in=$(this).find(".money_set").val();
		if(money_in==0||money_in=='0'||money_in==null||money_in==""||money_in==undefined){
			alert("请选择新用户得到优惠券金额!");
			//layer.closeAll();
			re_count =1;
			return;
		}
		new_oldstr+="0&"+select_in+":"+money_in+",";
	})

	$("#rules_old").nextAll().each(function(){
		var select_in=$(this).find(".card_info option:selected").val();
		if(select_in==0||select_in=='0'){
			alert("请选择老用户得到优惠券类型!");
			re_count =1;
			return false;
		}
		var money_in=$(this).find(".money_set").val();
		if(money_in==0||money_in=='0'||(!isNotNull(money_in))){
			alert("请选择老用户得到优惠券金额!");
			re_count =1;
			return false;
		}
		new_oldstr+="1&"+select_in+":"+money_in+",";
	})
	if(re_count==1){
		return;
	}
	if(title_share==""){
		alert("请输入分享标题!");
		return false;
	}
	if(share_url==""){
		alert("请输入活动详情页背景图!");
		return false;
	}
	if(share_concent==""){
		alert("请输入分享内容!");
		return false;
	}
	top.layer.confirm('是否确认提交分享配置？',{icon: 3,title:'提示信息'},function(index){
	var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
		console.log(loginname + "','" + loginkey + "','" + title_share+"==="+share_url+"==="+share_concent+"==="+new_oldstr+"=="+card_end_time);
		setTimeout(function(){
			$.ajax({
				url: "../../../../service",
				cache: false,
				data: {
					proc: "xmy_zyr.submit_share_config",
					para: "['" + loginname + "','" + loginkey + "','" + title_share + "','" + share_url + "','" + share_concent + "','" + new_oldstr+ "','" + card_end_time+ "']"
				},
				dataType: "json",
				success: function(data) {
					console.log(JSON.stringify(data));
					if(data[0].TAG=='0'){
						top.layer.alert(""+data[0].MSG+"",function(index){
						   top.location.href="login.html";
						   layer.close(index);
						});
					}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
						top.layer.msg("修改失败，请检查您的网络环境!");
						console.log("读取接口失败,参数错误");
					}else if(data[0].TAG == 1){
						top.layer.msg(""+data[0].MSG+"");
					}
				}
			});
		},1000);
	});
}
